capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

/* 
Figure B18
*/

local data "" ;
local output "" ;

use "prworaexact_cumul_compiled_264_poly1allcov_iv_all.dta", clear ;
drop _merge ;
rename _b_n_age18unfav  b_endo ;
rename _se_n_age18unfav se_endo ;

gen trt_level = b_endo + mean ;
gen hi95_endo = b_endo + 1.96*se_endo ;
gen lo95_endo = b_endo - 1.96*se_endo ;
gen hi95_trt = trt_level + 1.96*se_endo ;
gen lo95_trt = trt_level - 1.96*se_endo ;

gen age = substr(var,-2,2) 		if substr(var,1,9)  ==  "c_combpay" ;
replace age = substr(var,-8,2)  if substr(var,1,12) ==  "c_crime2char" | substr(var,1,12) ==  "c_crime2genr" ;
destring age, replace ;

gen 	type = "combpay" 	if substr(var,1,9)  ==  "c_combpay" ;
replace type = substr(var,9,5) if type == "" & (substr(var,9,5) == "chart" | substr(var,9,5) == "genrt") ;
replace type = substr(var,9,4) if type == "" & (substr(var,9,4) == "char"  | substr(var,9,4) == "genr") ;

keep type age b_endo se_endo hi95* lo95* mean trt_level ;

graph set window fontface "Helvetica";

local genr_title "Cumulative likelihood of income-generating charge" ;
local genrt_title "Cumulative number of income-generating charges" ;

local genr_ttext "ttext(.183 36 `"Control"', size(small)) ttext(.333 28  `"Treated"', size(small))" ;
local genrt_ttext "ttext(.4 36 `"Control"', size(small)) ttext(.8 28  `"Treated"', size(small))" ;

local genr_arrow "(pcarrowi .21 34.2 .19 35 "", mstyle(none) lwidth(vthin) color(black)) (pcarrowi .32 28 .3 29.5 "", mstyle(none) lwidth(vthin) color(black))" ;
local genrt_arrow "(pcarrowi .48 34.2 .42 35 "", mstyle(none) lwidth(vthin) color(black)) (pcarrowi .75 28 .7 29.5 "", mstyle(none) lwidth(vthin) color(black))" ;

foreach type in genr genrt { ;

	twoway 
		(scatter trt_level age 			if type == "`type'" , color(orange orange*.5) msymbol(X) msize(large)) 
		(rcap hi95_trt lo95_trt age 	if type == "`type'" , color(orange*.25))
		(scatter mean age 				if type == "`type'" , color(blue blue*.5)) 
		
		``type'_arrow'
		, 
			title("``type'_title'", size(medsmall))
			legend(off)
			xsc(r(18 38)) xlab(18(2)38) xtitle("Age", size(medsmall)) 
			ytitle("Control and treatment levels", size(medsmall))
			graphregion(color(white))
			bgcolor(white)
			ylabel(,nogrid angle(0))
			plotregion(margin(0 2 0 2))  
			``type'_ttext'
			;
			graph export "`output'/figB18_`type'.png", width(1000) height(750) replace ;
	
} ;

